﻿using System;
using System.Collections.Generic;
using System.Text;
using AAA.Base;
using System.Web.UI.WebControls;
using System.Data;

namespace AAA.Web
{
    public class DropDownListHelper
    {
/*
        public static string[] GetSelectedItems(DropDownList ddlList)
        {
            return GetSelectedItems(ddlList, CheckFieldEnum.Value);
        }

        public static string[] GetSelectedItems(DropDownList ddlList, CheckFieldEnum eReturnField)
        {
            List<string> lstSelectedItem = new List<string>();

            for (int i = 0; i < clstList.Items.Count; i++)
            {
                if (!clstList.Items[i].Selected)
                    continue;

                lstSelectedItem.Add(eReturnField == CheckFieldEnum.Item
                                        ?   clstList.Items[i].Text
                                        :   clstList.Items[i].Value);
            }

            return (string[])lstSelectedItem.ToArray();
        }
*/
        public static void FillItems(DropDownList ddlList, string[] strItems)
        {
            FillItems(ddlList, strItems, strItems);
        }

        public static void FillItems(DropDownList ddlList, string[] strItems, string[] strValues)
        {
            FillItems(ddlList, strItems, strValues, true);
        }

        public static void FillItems(DropDownList ddlList, string[] strItems, string[] strValues, bool isAppendAllItem)
        {
            if(isAppendAllItem)
                ddlList.Items.Add(new ListItem("全部", "-1"));
            for (int i = 0; i < strItems.Length; i++)
            {
                ddlList.Items.Add(new ListItem(strItems[i], strValues[i]));
            }
                
            if(ddlList.Items.Count > 0)
                ddlList.SelectedIndex = 0;
        }

        public static void FillItems(DropDownList ddlList, DataTable dtSource, string strItemField)
        {
            FillItems(ddlList, dtSource, strItemField, strItemField);
        }

        public static void FillItems(DropDownList ddlList, DataTable dtSource, string strItemField, string strValueField)
        {
            FillItems(ddlList, dtSource, strItemField, strValueField, true);
        }

        public static void FillItems(DropDownList ddlList, DataTable dtSource, string strItemField, string strValueField, bool isAppendAllItem)
        {
            ddlList.Items.Clear();
            ListItem lstItem;

            if (isAppendAllItem)
                ddlList.Items.Add(new ListItem("全部", "-1"));

            for (int i = 0; i < dtSource.Rows.Count; i++)
            {
                lstItem = new ListItem(dtSource.Rows[i][strItemField].ToString(),
                                       dtSource.Rows[i][strValueField].ToString());
                ddlList.Items.Add(lstItem);
            }

            if (ddlList.Items.Count > 0)
                ddlList.SelectedIndex = 0;
        }


    }
}
